package com.stylefeng.guns.core.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;

/** 百度地图坐标和火星坐标转换 
 * Created by 明明如月 on 2017-03-22. 
 */ 
public class CoodinateCovertor {
	private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
	private static String gdUrl="https://restapi.amap.com/v3/geocode/geo?key=67c7d7eb7a322480a2fa3d77f03bde70";
	private static String gdDistanceUrl="https://restapi.amap.com/v3/direction/driving?key=67c7d7eb7a322480a2fa3d77f03bde70";
	
	 
	/** 
	* 对double类型数据保留小数点后多少位 
	*  高德地图转码返回的就是 小数点后6位，为了统一封装一下 
	* @param digit 位数 
	* @param in 输入 
	* @return 保留小数位后的数 
	*/  
	public static double dataDigit(int digit,double in){  
	    return new   BigDecimal(in).setScale(digit,   BigDecimal.ROUND_HALF_UP).doubleValue(); 
	}  
	 
	/** 
	 * 将火星坐标转变成百度坐标 
	 * @param lngLat_gd 火星坐标（高德、腾讯地图坐标等） 
	 * @return 百度坐标 
	*/    
	public static LngLat bd_encrypt(LngLat lngLat_gd)  
	{  
		double x = Double.parseDouble(lngLat_gd.getLng()), y = Double.parseDouble(lngLat_gd.getLat());  
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);  
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x *  x_pi);  
		return new LngLat(dataDigit(6,z * Math.cos(theta) + 0.0065),dataDigit(6,z * Math.sin(theta) + 0.006));  
	}  
	/** 
	 * 将百度坐标转变成火星坐标 
	 * @param lngLat_bd 百度坐标（百度地图坐标） 
	 * @return 火星坐标(高德、腾讯地图等) 
	*/  
	public static LngLat bd_decrypt(LngLat lngLat_bd)  
	{  
		double x = Double.parseDouble(lngLat_bd.getLng()) - 0.0065, y = Double.parseDouble(lngLat_bd.getLat()) - 0.006;  
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);  
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);  
		return new LngLat( dataDigit(7,z * Math.cos(theta)),dataDigit(7,z * Math.sin(theta))); 	
	}  
	  
	public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
	    double[] gd_lat_lon = new double[2];
	    double PI = 3.14159265358979324 * 3000.0 / 180.0;
	    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
	    double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
	    double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
	    gd_lat_lon[0] = z * Math.cos(theta);
	    gd_lat_lon[1] = z * Math.sin(theta);
	    return gd_lat_lon;
	 }
	public static void main(String[] args) {  
		/*LngLat lngLat_bd = new LngLat(98.454342,24.951419);  
		System.out.println(bd_decrypt(lngLat_bd)); 
		double[] d=bdToGaoDe(24.951419,98.454342);
		System.out.println(d[0]);
		System.out.println(d[1]);*/
	/*	System.out.println(getGpdCoorByAddress("昆明市西北部汽车客运站"));
		System.out.println(getGpdCoorByAddress("昆明市行政学院(学苑路)"));
		System.out.println(getGpdCoorByAddress("昆明市五华区云上四季连锁酒店(昆明海源中路店)"));
		System.out.println(getGpdCoorByAddress("水闸口农贸市场停车场"));*/
		
		System.out.println(getDistanceByPoint("102.706216,25.037192","102.719691,25.038281"));
		
	}
	public static String getGpdCoorByAddress(String address){
		String msgResult="";
		try {
			String result=HttpUtil.doGet(gdUrl+"&address="+URLEncoder.encode(address, "utf-8"), null,"utf-8");			
			if(result!=null && result.length()>0){
				JSONObject obj=JSONObject.parseObject(result);
				if(obj.getString("status").equals("1")){					
					msgResult=obj.getJSONArray("geocodes").getJSONObject(0).getString("location");
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msgResult;
	}
	public static String getDistanceByPoint(String spoint,String epoint){
		String distance="0";		
		String result=HttpUtil.doGet(gdDistanceUrl+"&strategy=0&origin="+spoint+"&destination="+epoint, null,"utf-8");			
		
		if(result!=null && result.length()>0){
			JSONObject obj=JSONObject.parseObject(result);
			if(obj.getString("status").equals("1")){					
				distance=obj.getJSONObject("route").getJSONArray("paths").getJSONObject(0).getString("distance");
				System.out.println(distance);
				distance=""+new BigDecimal(distance).divide(new BigDecimal(1000), 2,RoundingMode.HALF_UP);
			}
		}		
		return distance;
	}
}
